package cn.tianyigps.obd.utils;


import cn.tianyigps.obd.beans.LoadMessage;

import java.util.Map;
import java.util.Optional;

/**
 * Created with IntelliJ IDEA
 * Created By yangxl
 * Date: 2019/2/22
 * Time: 18:05
 * Description:载重的削峰工具类
 */
public class LoadCutPeakUtil {
    /**
     * @Description: 车联行驶过程中异常数据抖动（三个点去判断削峰）
     * @params: [points]
     * @return: java.lang.Double
     * @auther: yangxl
     * @date: 2019/2/23 9:10
     */
    public static void cutPeakWeightByThreePoint(Map<String, Double> points, LoadMessage loadMsg, Double speed, Map<String, String> map) {
        //当亲一个点和第二个点存在时才能做校验
        if (Optional.ofNullable(points.get("one")).isPresent()
                && Optional.ofNullable(points.get("two")).isPresent() && Optional.ofNullable(points.get("three")).isPresent()) {
            if (speed > 10 && Optional.ofNullable(loadMsg.getBumpyValue()).isPresent()) {
                //需要校验的重量条件(第一个点和第三个点偏差小于某个值才能进行削峰)
                Double maxDeviation = points.get("three") / loadMsg.getBumpyValue();

                Double diffValue = points.get("three") - points.get("one") >= 0 ? points.get("three") - points.get("one") : points.get("one") - points.get("three");
                //三个行成v或者^字型进行削峰
                Boolean peakCondition = (points.get("two") > points.get("one") && points.get("two") > points.get("three")) || (points.get("two") < points.get("one") && points.get("two") < points.get("three"));
                if (diffValue <= maxDeviation && peakCondition) {
                    map.put("cutPeakValue", String.valueOf((points.get("one") + points.get("three")) / 2));
                } else {
                    map.put("cutPeakValue", String.valueOf(points.get("two")));
                }
            } else {
                map.put("cutPeakValue", String.valueOf(points.get("two")));
            }
        }
    }
}
